<?php
$sMsg="";
$Action="?mod=job.listcat.add&method=add";
$Submit="Đồng ý";
$TitleTable="Tạo mới";
if(isset($_GET['pid'])){ $pid = $_GET['pid'];}else{ $pid=0;}
if ($Method=="add")
{
	if(isset($_POST["ltd_ten"])) {$ltd_ten=addslashes($_POST["ltd_ten"]);}
	if(isset($_POST["ltd_id_cha"])) $ltd_id_cha=$_POST["ltd_id_cha"];
	if(isset($_POST["ltd_thutu"])) $ltd_thutu=$_POST["ltd_thutu"];
	if(isset($_POST["ltd_hienthi"])) $ltd_hienthi=$_POST["ltd_hienthi"];
	if (trim($ltd_ten)!="")
	{
			$sql="Insert into ".$prefix."loaituyendung(ltd_ten,ltd_id_cha,ltd_thutu,ltd_hienthi) values('$ltd_ten',$ltd_id_cha,$ltd_thutu,$ltd_hienthi)";
			//echo $sql;
			mysql_query($sql);
			Redirect("Đã thêm mới thành công","?mod=job.listcat");
	} 
}
if($Method=="edit")
{
	$id=$_GET["id"];
	$pid=$_GET["pid"];
	$sql="select * from ".$prefix."loaituyendung where ltd_id=$id";
	$result=mysql_query($sql);
	if(mysql_num_rows($result))
	{
		$rows=mysql_fetch_array($result);
		$ltd_ten=stripslashes($rows["ltd_ten"]);
		$ltd_id_cha=$rows["ltd_id_cha"];
		$ltd_thutu=$rows["ltd_thutu"];
		$ltd_hienthi=$rows["ltd_hienthi"];
		$Submit="Cập nhập";
		$TitleTable="Sửa thông tin";
		$Action="?mod=job.listcat.add&method=update&pid=$pid&id=$id";
	}
}

if ($Method=="update")
{
	$Submit="Tạo Mới";
	$id=$_GET["id"];
	$pid=$_GET['pid'];
	if(isset($_POST["ltd_ten"])) {$ltd_ten=addslashes($_POST["ltd_ten"]);}
	if(isset($_POST["ltd_id_cha"])) $ltd_id_cha=$_POST["ltd_id_cha"];
	if(isset($_POST["ltd_thutu"])) $ltd_thutu=$_POST["ltd_thutu"];
	if(isset($_POST["ltd_hienthi"])) $ltd_hienthi=$_POST["ltd_hienthi"];
	if (trim($ltd_ten)!="")
	{
			$sql="update ".$prefix."loaituyendung set ltd_ten='$ltd_ten',ltd_id_cha=$ltd_id_cha,ltd_thutu=$ltd_thutu,ltd_hienthi=$ltd_hienthi where ltd_id=$id";
			//echo $sql;
			mysql_query($sql);
			Redirect("Cập nhập thành công","?mod=job.listcat");
	} 
}
?>
<script language="javascript1.2">
	function CheckFrm(_form)
	{
	if (_form.ltd_ten.value==""){alert("Bạn phải nhập tên danh mục");_form.ltd_ten.focus();return false;}
	if (_form.ltd_thutu.value==""){alert("Bạn phải nhập thứ tự sắp xếp");_form.ltd_thutu.focus();return false;}
	if (_form.ltd_hienthi.value==""){alert("Bạn phải chọn trạng thái");_form.ltd_hienthi.focus();return false;}
	}
</script>
<?
	$sql = "select count(*) as total from ".$prefix."loaituyendung".$where."";
	$result = mysql_query($sql) or die("Can't connect database");
	$row = mysql_fetch_array($result);
	?>
<!--Bat dau form tao sua -->
<form name="frmHuyen" id="frmHuyen" action="<?=$Action;?>" method="post" onSubmit="return CheckFrm(this);">
	<table width="100%" border="1" style="border-collapse:collapse " bordercolor="#B2DAEE">
	  <tr class="newsList-content-head">
		<td height="28" colspan="2" align="center"><?=$TitleTable?></td>
	  </tr>		  
	  <tr>
	      <td width="254" align="right"><strong>
		  Tên danh mục :</strong></td>
	      <td width="497">
	      <input type="text" name="ltd_ten" value="<?=$ltd_ten;?>" size="40" class="TextBox"> *	      </td>
	  </tr>
	  <tr>
		<td width="254" align="right"><strong>Danh mục cha :</strong></td>
		<td width="497">
		<select name="ltd_id_cha" id="ltd_id_cha" style="width:120px">
		<option value="0" <? if($pid == 0){ ?> selected="selected" <? } ?>>Không có</option>
		<?
		$pSql = "select * from ".$prefix."loaituyendung WHERE ltd_id_cha = 0 order by ltd_id desc";
		$rs= mysql_query($pSql);
		while($r = mysql_fetch_array($rs)){
		?>
		<option value="<?=$r['ltd_id']?>" <? if($pid == $r[ltd_id]){?> selected="selected" <? } ?>><?=$r['ltd_ten']?></option>
		<? }?>
		</select>
	
		</td>
	  </tr>
	  <tr>
		<td width="254" align="right"><strong>Thứ tự :</strong></td>
		<td width="497">
		<input type="text" name="ltd_thutu" value="<? if($ltd_thutu=="") { echo $row['total']+1;}else { echo $ltd_thutu;}?>" size="10" class="TextBox">*</td>
	  </tr>	  
	  <tr>
		<td width="254" align="right"><strong>Trạng thái :</strong></td>
		<td width="497">
		<select name="ltd_hienthi" id="ltd_hienthi" style="width:100px">
		<option value="1">Kích hoạt</option>
		<option value="0">Không kích hoạt</option>
		</select>
			<script language="javascript">
		 for(var i=0;i<document.frmHuyen.ltd_hienthi.length;i++)
		 {if(document.frmHuyen.ltd_hienthi[i].value=="<?=$ltd_hienthi?>")
		 {document.frmHuyen.ltd_hienthi.selectedIndex=i;break;}}
		</script>
		</td>
	  </tr>
	  <tr>
		<td colspan="2" align="center">
		<input type="submit" name="Submit" value="<?=$Submit?>" class="Button" onClick="return CheckFrmTl();">&nbsp;&nbsp;&nbsp;
		<input type="reset" name="reset" value="Làm lại" class="Button">&nbsp;&nbsp;&nbsp;
		<input type="button" name="back" value="Quay về" class="Button" onclick="GoBack('?mod=job.listcat')"></td>
	  </tr>
	</table>
</form>
	